package com.zimi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.model.WfEntrust;
import com.zimi.model.WfEntrustVO;
import com.zimi.service.WfEntrustService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("工作委托")
@RestController
@RequestMapping("wf/entrust")
public class WfEntrustApi {

    @Autowired
    private WfEntrustService service;

    @PostMapping("list")
    @ApiOperation("查询所有")
    public List<WfEntrust> selectAll(WfEntrust record) {
        return service.selectAll(record);
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public Object selectByPrimaryKey(@PathVariable("id") int id) {
        Map map = new HashMap();
        WfEntrust wfEntrust = service.selectByPrimaryKey(id);
        map.put("OK", "OK");
        map.put("wfEntrust", wfEntrust);
        map.put("code", "200");
        return map;
    }

    @PostMapping("del/{id}")
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        return service.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @PostMapping("add")
    @ApiOperation("添加")
    public boolean insert(WfEntrust record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PostMapping("update")
    @ApiOperation("根据条件修改信息")
    public boolean updateByPrimaryKey(WfEntrust record) {
        return service.updateByPrimaryKeySelective(record) > 0 ? true : false;
    }

    @PostMapping("pageList")
    @ApiOperation("工作委托分页查询[sql中委托人和被委托人之间是用or链接不是and]")
    public Map getMyWfTask(WfEntrust record) {
        PageHelper.startPage(record.page, record.limit);
        List<WfEntrust> list = service.selectAll(record);
        PageInfo<WfEntrust> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("total", pageInfo.getTotal());
        map.put("rows", list);
        return map;
    }

    @PostMapping("add/entrust")
    @ApiOperation("添加工作委托")
    public Map insetEntrust(WfEntrustVO record) {
        Map<String, Object> map = new HashMap<>();
        boolean flag = false;
        String wfId = record.getWfId();
        String[] str = wfId.split(",");
        List<WfEntrustVO> list = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            record.setWfId(str[i]);
            list.addAll(service.selectEntrustVO(record));
        }
        for (WfEntrustVO wfEntrustVO : list) {
            if (record.getConsignor().equals(wfEntrustVO.getConsignor())) {
                flag = true;
                break;
            }
            if (record.getMandatary().equals(wfEntrustVO.getMandatary())) {
                flag = true;
                break;
            }
        }
        if (flag) {
            map.put("OK", "ERROR");
        } else {
            service.insertSelective(record);
            map.put("OK", "OK");
        }
        return map;
    }

}
